package com.platform.car.controller;

import com.platform.common.config.Global;
import com.platform.common.page.Page;
import com.platform.car.model.Parking;
import com.platform.car.model.ParkingZone;
import com.platform.car.service.ParkingService;
import com.platform.car.service.ParkingZoneService;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsPicture;
import com.platform.upms.model.UpmsUser;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car/parkingZone")
public class ParkingZoneController {

    @Autowired
    ParkingZoneService parkingZoneService;

    @Autowired
    ParkingService parkingService;

    @Autowired
    UpmsPictureService upmsPictureService;
    /**
     * 带分页的查询列表
     * @param page
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(Page<ParkingZone> page,ParkingZone parkingZone, ModelMap modelMap,HttpSession session){
        if(!UserUtils.isHighestPower()) {
            parkingZone.setOrgId(UserUtils.getUserOrgId());
        }
        Page<ParkingZone> parkingZoneList = parkingZoneService.findByPage(page,parkingZone);
        modelMap.addAttribute("parkingZoneList",parkingZoneList);
        return  "/car/parkingZone/list.jsp";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(ModelMap modelMap,HttpSession session){
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer orgId = upmsUser.getOrgId();
        Parking parking=new Parking();
        if(orgId !=null){
            parking.setOrgId(orgId);
        }
        List<Parking> parkingList = parkingService.findList(parking);
        modelMap.put("parkingList",parkingList);
        if(parkingList.size()>0){
            modelMap.put("totalFloor",parkingList.get(0).getFloor());
        }
        return "/car/parkingZone/add.jsp";
    }

    /**
     * 保存数据
     * @param parkingZone
     * @return
     */
    @RequestMapping("/saveData")
    @ResponseBody
    public Tip saveData(ParkingZone parkingZone){
        try{
            parkingZone.setCreateTime(new Date());
            parkingZoneService.saveData(parkingZone);
            return  new Tip("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"保存失败");
        }
    }

    /**
     * 修改页面
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id,ModelMap modelMap,HttpSession session){
        ParkingZone parkingZone = parkingZoneService.get(id);
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer orgId = upmsUser.getOrgId();
        Parking p=new Parking();
        if(orgId !=null){
            p.setOrgId(orgId);
        }
        List<Parking> parkingList = parkingService.findList(p);
        modelMap.put("parkingList",parkingList);
        Parking parking = parkingService.get(parkingZone.getParkingId());
        modelMap.put("totalFloor",parking.getFloor());
        modelMap.addAttribute("parkingZone",parkingZone);
        if(parkingZone.getImgId() !=null &&  !"".equals(parkingZone.getImgId())){
            UpmsPicture upmsPicture = upmsPictureService.get(parkingZone.getImgId());
            modelMap.put("zoneUrl",upmsPicture.getPicUrl());
        }
        if(parkingZone.getImgZone() !=null &&  !"".equals(parkingZone.getImgZone())){
            UpmsPicture upmsPicture = upmsPictureService.get(parkingZone.getImgZone());
            modelMap.put("lotUrl",upmsPicture.getPicUrl());
        }
        return "/car/parkingZone/edit.jsp";
    }


    /**
     * 修改数据
     * @param parkingZone
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Tip update(ParkingZone parkingZone){
        try{
            parkingZoneService.update(parkingZone);
            return  new Tip("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"修改失败");
        }
    }

    /**
     * 删除数据
     * @param ids
     * @return
     */
    @RequestMapping("/deleteDataByIds")
    @ResponseBody
    public Tip deleteDataByIds(String ids[]){
        try {
            parkingZoneService.deleteDataByIds(ids);
            return  new Tip("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"删除失败");
        }
    }

    /**
     * 选择图片
     * @return
     */
    @RequestMapping("/chooseImg")
    public String chooseImg(HttpSession session, ModelMap modelMap, Page page){
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer id = upmsUser.getId();//当前用户的ID
        UpmsPicture picture=new UpmsPicture();
        picture.setUserId(id);
        Page<UpmsPicture> pictureList =upmsPictureService .findByPage(page,picture);
        modelMap.addAttribute("pictureList",pictureList);
        return  "/car/parkingZone/imgZoneList.jsp";
    }


    /**
     * 根据停车场ID查询分区
     * @param parkingId
     * @return
     */
    @RequestMapping("/selectByParkingId")
    @ResponseBody
    public List<ParkingZone> selectByParkingId(Integer parkingId){
        List<ParkingZone> parkingZoneList = parkingZoneService.selectByParkingId(parkingId);
        return  parkingZoneList;
    }


    /**
     * 查询这个分区的图片
     * @param id 分区id
     * @return 分区
     */
    @RequestMapping(value="/selectZoneImg")
    @ResponseBody
    public ParkingZone selectZoneImg(Integer id) {
        ParkingZone parkingZone =parkingZoneService.selectZoneImg(id);
        return parkingZone;
    }
}
